package com.ihg.hiex.admin.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.service.account.AdministratorService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.Status;
import com.ihg.hiex.entity.user.Administrator;

/**
 * 
 * @author Wilson
 * 
 */
@Controller
@MenuSetting("menu-home")
public class HomeController {
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private AdministratorService administratorService;
    
    /**
     * list active session
     * 
     * @param login
     * @return
     */
    @RequestMapping("/home")
    public ModelAndView home(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView(login.getAuthorityAlias() + "-home");
        
        if (login.isAdministrator()) {
            
            Administrator administrator = administratorService.load(Administrator.class, login.getId());
            List<Session> sessions = sessionService.list(Status.ACTIVE, administrator);
            modelAndView.addObject("sessions", sessions);
            
        } else if (login.isSupervisor()) {
            
            List<Session> activeSessions = sessionService.list(Status.ACTIVE);
            modelAndView.addObject("activeSessions", activeSessions);
            
            List<Session> historySessions = sessionService.list(Status.HISTORY);
            modelAndView.addObject("historySessions", historySessions);
            
        } else if (login.isFacilitator()) {
            modelAndView.setViewName("redirect:/session-meetings");
        } else if(login.isCoach()) {
            modelAndView.setViewName("redirect:/coach-coaching-progress");
        }
        
        return modelAndView;
        
    }
    
    /**
     * @return
     */
    @RequestMapping("/settings")
    public String settings() {
        return "redirect:/administrators";
    }
    
}
